﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Logistics.Utility
{
    /// <summary>
    /// 缓存值类
    /// </summary>
    public class FieldValueCollection
    {
        private Dictionary<string, object> fields = new Dictionary<string, object>();
        private static object lockHelper = new object();
        private static volatile FieldValueCollection instance = null;
        public FieldValueCollection()
        {
        }
        public static FieldValueCollection Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (lockHelper)
                    {
                        if (instance == null)
                        {
                            instance = new FieldValueCollection();
                        }
                    }
                }
                return instance;
            }
        }

        #region IDictionary 成员
        /// <summary>
        /// 向集合中添加元素
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(string key, object value)
        {
            if (!Contains(key))
                fields.Add(key, value);
            else
                fields[key] = value;
        }
        /// <summary>
        /// 移除集合中所有元素
        /// </summary>
        public void Clear()
        {
            fields.Clear();
        }
        /// <summary>
        /// 判断集合中是否有该元素
        /// </summary>
        /// <param name="key"></param>
        /// <returns>有返true;没有返回false</returns>
        public bool Contains(string key)
        {
            return fields.ContainsKey(key);
        }


        public IDictionaryEnumerator GetEnumerator()
        {
            return fields.GetEnumerator();
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public ICollection Keys
        {
            get { return fields.Keys; }
        }
        /// <summary>
        /// 移除集合中元素
        /// </summary>
        /// <param name="key"></param>
        public void Remove(string key)
        {
            fields.Remove(key);
        }
        /// <summary>
        /// 获取集合中值
        /// </summary>
        public ICollection Values
        {
            get { return fields.Values; }
        }

        /// <summary>
        /// 获取key的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object this[string key]
        {
            get
            {
                if (Contains(key))
                    return fields[key];
                else
                    return null;
            }
            set
            {
                if (Contains(key))
                    fields[key] = value;
                else
                    fields.Add(key, value);
            }
        }

        #endregion

        #region ICollection 成员
        /// <summary>
        /// 将Dictionary复到Array中
        /// </summary>
        /// <param name="array"></param>
        /// <param name="index"></param>
        public void CopyTo(Array array, int index)
        {
            foreach (string key in fields.Keys)
            {
                FieldValue fv = new FieldValue(key, fields[key]);
                array.SetValue(fv, index);
                index++;
            }
        }
        /// <summary>
        /// 集合中所有元素的记录
        /// </summary>
        public int Count
        {
            get { return fields.Count; }
        }

        public bool IsSynchronized
        {
            get { return false; ; }
        }

        public object SyncRoot
        {
            get { return this; }
        }

        #endregion


    }
}
